package com.cooper.controllers.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.cooper.controllers.Template;
import com.cooper.core.JsonResult;
import com.cooper.service.admin.MallCategoryService;
import com.cooper.util.Controllers;
import com.cooper.vo.MallCategoryVo;
import com.cooper.vo.TreeNodeVo;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.portal.Portal;

public class MallCategoryController {

	@Autowired
	MallCategoryService  mallCategoryService;
	
//	@Autowired
//	MallCategorySearchService  mallCategorySearchService;
	
	@Get("getDirectChilds")
	@Post("getDirectChilds")
	public Object getMallCategorysByParentId(JsonResult json,@Param("id") String parentId) {
		MallCategoryVo searchVo = new MallCategoryVo();
		if(StringUtils.isEmpty(parentId)){
			searchVo.setParentId(-1l);
		}else{
			searchVo.setParentId(new Long(parentId));
		}
		List<MallCategoryVo>  ret = mallCategoryService.findMallCategorys(searchVo);
		return JSONObject.toJSON(assembleTreeNodes(ret));
	}
	
	@Get("getDirectChildsCheck")
	@Post("getDirectChildsCheck")
	public Object getMallCategorysCheckByParentId(JsonResult json,@Param("id") String parentId) {
		MallCategoryVo searchVo = new MallCategoryVo();
		if(StringUtils.isEmpty(parentId)){
			searchVo.setParentId(0L);
		}else{
			searchVo.setParentId(new Long(parentId));
		}
		List<MallCategoryVo>  ret = mallCategoryService.findMallCategorysCheck(searchVo);
		return JSONObject.toJSON(assembleTreeNodes(ret));
	}
	
	@Get("checkTree")
	public String tree(Invocation inv) throws Exception{
		return "MallCategoryCheckTree";
	}
	
	@Template(title="类目管理")
	@Get 
	public String show(Invocation inv, Portal portal){
		return "MallCategoryMain";
	}
	
	@Post("add")
	public Object add(Invocation inv, @Param("parentId") String parentId,@Param("name") String name) {
		JsonResult json = new JsonResult();
		
		try{
			if(StringUtils.isNotEmpty(parentId)){
				MallCategoryVo parent = mallCategoryService.findMallCategoryById(new Long(parentId));
				if(parent!=null){
					MallCategoryVo addItem = new MallCategoryVo();
					Controllers.prepareBaseParamForDataSaving(addItem, inv, false);
					addItem.setName(name);
					addItem.setCataLevel(parent.getCataLevel()+1);
					addItem.setHasChild(0);
					addItem.setParentId(parent.getId());
					int maxSortNo = mallCategoryService.findMaxSorNoByParentId(parent.getId());
					maxSortNo++;
					addItem.setSortNo(String.valueOf(maxSortNo));
					
					long addedItem = mallCategoryService.saveMallCategory(addItem);
					parent.setHasChild(1);
					mallCategoryService.saveMallCategory(parent);
					
					//保存全路径
					MallCategoryVo current = mallCategoryService.findMallCategoryById(addedItem);
					current.setFullIdPath(parent.getFullIdPath()+"/"+current.getId());
					current.setFullNamePath(parent.getFullNamePath()+"/"+current.getName());
					mallCategoryService.saveMallCategory(current);
					
					TreeNodeVo node = new TreeNodeVo();
					node.setId(addedItem);
					node.setLevel(addItem.getCataLevel());
					node.setpId(addItem.getParentId());
					node.setName(addItem.getName());
					json.put("dataNode", node);
				}
				
			}
			
			json.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			json.setSuccess(false);
			json.setMessage(e.getMessage());
		}
		return json;
	}
	
	@Post("rename")
	public Object rename(Invocation inv, @Param("id") String id, @Param("name") String name) {
		JsonResult json = new JsonResult();
		
		try{
			if(StringUtils.isNotEmpty(id)){
				MallCategoryVo current = mallCategoryService.findMallCategoryById(new Long(id));
				if(current!=null){
					current.setName(name);
					long addedItem = mallCategoryService.saveMallCategory(current);
					TreeNodeVo node = new TreeNodeVo();
					node.setId(addedItem);
					node.setLevel(current.getCataLevel());
					node.setpId(current.getParentId());
					node.setName(current.getName());
					json.put("dataNode", node);
				}
			}
			
			json.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			json.setSuccess(false);
			json.setMessage(e.getMessage());
		}
		return json;
	}
	

	@Post("reorder")
	public Object reorder(Invocation inv, @Param("id") String id,@Param("targetId") String targetId) {
		JsonResult json = new JsonResult();
		try{
			if(StringUtils.isNotEmpty(id)){
				MallCategoryVo current = mallCategoryService.findMallCategoryById(new Long(id));
				MallCategoryVo target = mallCategoryService.findMallCategoryById(new Long(targetId));
				if(current!=null&&target!=null&&current.getParentId()==target.getParentId()){
					String currOrder = current.getSortNo();
					String targetOrder = target.getSortNo();
					current.setSortNo(targetOrder);
					target.setSortNo(currOrder);
					mallCategoryService.saveMallCategory(current);
					mallCategoryService.saveMallCategory(target);
					
				}
				
			}
			
			json.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			json.setSuccess(false);
			json.setMessage(e.getMessage());
		}
		return json;
	}
	@Post("remove")
	public Object remove(Invocation inv, @Param("id") String id) {
		JsonResult json = new JsonResult();
		
		try{
			if(StringUtils.isNotEmpty(id)){
				MallCategoryVo current = mallCategoryService.findMallCategoryById(new Long(id));
				if(current!=null){
					current.setDeleted("y");
					mallCategoryService.saveMallCategory(current);
					MallCategoryVo parent = mallCategoryService.findMallCategoryById(current.getParentId());
					if(parent.getHasChild()==1){
						MallCategoryVo searchVo = new MallCategoryVo();
						searchVo.setParentId(parent.getId());
						List<MallCategoryVo>  ret = mallCategoryService.findMallCategorys(searchVo);
						if(ret==null||ret.isEmpty()){
							parent.setHasChild(0);
						}
						mallCategoryService.saveMallCategory(parent);
					}
					
					
				}
				
			}
			
			json.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			json.setSuccess(false);
			json.setMessage(e.getMessage());
		}
		return json;
	}
	
	private List<TreeNodeVo> assembleTreeNodes(List<MallCategoryVo> ret){
		List<TreeNodeVo> finalRet = new ArrayList<TreeNodeVo>();
		if(ret!=null&&!ret.isEmpty()){
			for(MallCategoryVo vo:ret){
				TreeNodeVo node = new TreeNodeVo();
				node.setId(vo.getId());
				node.setLevel(vo.getCataLevel());
				node.setpId(vo.getParentId());
				node.setName(vo.getName());
				node.setText(vo.getName());
				node.setIsParent(vo.getHasChild()==1);
				node.setChecked(vo.getChecked());
				finalRet.add(node);
			}
		}
		return finalRet;
	}
	
	@Post("filter")
	public Object filter(Invocation inv,@Param("keyWord") String name) {
		MallCategoryVo searchVo = new MallCategoryVo();
		searchVo.setName(name);
		List<MallCategoryVo> ret = mallCategoryService.findMallCategorys(searchVo);
		return JSONObject.toJSON(assembleTreeNodes(ret));
	}
	
	
//	@Post("saveMallCatSearch")
//	public Object saveMallCatSearch(Invocation inv, @Param("cid") String cid,@Param("itemId") String itemId,@Param("checked") boolean checked) {
//		JsonResult json = new JsonResult();
//		
//		try{
//			if(StringUtils.isNotEmpty(cid)&&StringUtils.isNotEmpty(itemId)){
//				if(checked){
//					mallCategorySearchService.saveMallCatSearch(new Long(cid), new Long(itemId));
//				}else{
//					MallCatSearchVo searchVo = mallCategorySearchService.getMallCatSearch(new Long(cid), new Long(itemId));
//					if(searchVo!=null){
//						searchVo.setDeleted("y");
//						mallCategorySearchService.saveMallCatSearch(searchVo);
//					}
//				}
//			}
//			
//			json.setSuccess(true);
//		}catch(Exception e){
//			e.printStackTrace();
//			json.setSuccess(false);
//			json.setMessage(e.getMessage());
//		}
//		return json;
//	}
}
